﻿// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Permissive License.
// See http://www.microsoft.com/resources/sharedsource/licensingbasics/sharedsourcelicenses.mspx.
// All other rights reserved.

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Data;
using System.Windows.Ink;
using System.Windows.Input;


namespace Expression.Samples.Interactivity.Design {
	public class InkStrokeConverter : IValueConverter {
		#region IValueConverter Members

		public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
			StrokeCollection strokeCollection = new StrokeCollection();

			List<Point> pointList = new List<Point>();

			string pointString = (string)value;
			if (pointString != null) {
				string[] points = pointString.Split(';');
				foreach (string p in points) {
					string[] xy = p.Split(',');
					try {
						pointList.Add(new Point(double.Parse(xy[0]), double.Parse(xy[1])));
					}
					catch {
					}
				}
			}
			if (pointList.Count > 0) {
				strokeCollection.Add(new Stroke(new StylusPointCollection(pointList)));
			}
			return strokeCollection;
		}

		public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
			StrokeCollection strokeCollection = (StrokeCollection)value;
			StringBuilder strokeString = new StringBuilder();

			foreach (Stroke stroke in strokeCollection) {
				foreach (Point point in stroke.StylusPoints) {
					strokeString.Append(point.X + "," + point.Y);
					strokeString.Append(";");
				}
			}
			strokeString.Remove(strokeString.Length - 1, 1);
			return strokeString.ToString();
		}

		#endregion
	}
}
